package com.control.qct.businesssteward.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.model.DoPayPromotionGoodsModel;
import com.control.qct.businesssteward.utils.DecimalFormatUtils;
import com.control.qct.businesssteward.utils.GlideUtil;

import java.util.List;

/**
 * Created by Administrator on 2018/3/24 0024.
 */

public class PaymentPromotionAdapter extends BaseQuickAdapter<DoPayPromotionGoodsModel, BaseViewHolder> {
    private IPromotionCallbackListener mIPromotionCallbackListener;

    public PaymentPromotionAdapter(@Nullable List<DoPayPromotionGoodsModel> data, IPromotionCallbackListener listener) {
        super(R.layout.rv_payment_promotion_item, data);
        mIPromotionCallbackListener = listener;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final DoPayPromotionGoodsModel item) {
        holder.setText(R.id.tv_promotion_type, item.getMarketingDescription());
        switch (item.getType()) {
            case 1:
                holder.setImageResource(R.id.iv_promotion_tag, R.drawable.icon_jiesuan_huangou);
                break;
            case 2:
                holder.setImageResource(R.id.iv_promotion_tag, R.drawable.icon_jiesuan_zengpinfangkuai);
                break;
        }
        final RecyclerView rvPromotionGoods = holder.getView(R.id.rv_promotion_goods);
        rvPromotionGoods.setLayoutManager(new LinearLayoutManager(mContext));
        final PromotionGoodsAdapter promotionGoodsAdapter = new PromotionGoodsAdapter(item.getItems(), item
                .getSelectWay());
        rvPromotionGoods.setAdapter(promotionGoodsAdapter);
        promotionGoodsAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CheckBox c = (CheckBox) adapter.getViewByPosition(rvPromotionGoods, position, R.id.cb_goods_select);
//                if (item.getSelectWay() == 1) {
                promotionGoodsAdapter.setChecked(position);
                for (int i = 0; i < item.getItems().size(); i++) {
                    item.getItems().get(i).setChecked(false);
                }
                item.getItems().get(position).setChecked(c.isChecked());
//                }
                mIPromotionCallbackListener.callbackPromotionResult(holder.getAdapterPosition(), item.getItems());
            }
        });

    }

    class PromotionGoodsAdapter extends BaseQuickAdapter<DoPayPromotionGoodsModel.ItemsBean, BaseViewHolder> {
        private int mPosition = -1;
        private int mSelectWay;

        public PromotionGoodsAdapter(@Nullable List<DoPayPromotionGoodsModel.ItemsBean> data, int selectWay) {
            super(R.layout.rv_payment_promotion_goods_item, data);
            mSelectWay = selectWay;
        }

        @Override
        protected void convert(BaseViewHolder holder, DoPayPromotionGoodsModel.ItemsBean item) {
            GlideUtil.loadImage(mContext,item.getImageSmallUrl(), (ImageView) holder.getView(R.id.iv_goods_picture));
            holder.setText(R.id.tv_goods_name, item.getDescription()).setText(R.id.tv_goods_code, "条码：" + item
                    .getBarcode()).setText(R.id.tv_goods_price, "原价：￥" + DecimalFormatUtils.DecimalFormatTwo(item
                    .getSysPrice())).setText(R.id.tv_discount_price, "￥" + DecimalFormatUtils.DecimalFormatTwo(item
                    .getPreferentialPrice())).setChecked(R.id.cb_goods_select, (mPosition == holder.getAdapterPosition
                    () && item.isChecked()) || mSelectWay == 0);

            CheckBox c = holder.getView(R.id.cb_goods_select);
            if (mSelectWay == 1) {
                holder.addOnClickListener(R.id.cb_goods_select);
                c.setEnabled(true);
            } else {
                c.setEnabled(false);
            }

        }

        public void setChecked(int position) {
            mPosition = position;
            notifyDataSetChanged();
        }
    }

    public interface IPromotionCallbackListener {
        void callbackPromotionResult(int position, List<DoPayPromotionGoodsModel.ItemsBean> item);
    }
}
